var curPage;        //当前页数
var totalItem;      //总记录数
var pageSize;       //每一页记录数
var totalPage;      //总页数
 
//获取分页数据
function turnPage(page)
{
  $.ajax({
    type: 'POST',
    url: commentDataUrl,     //这里是请求的后台地址，自己定义
    data: {'pageNum':page},
    dataType: 'json',
    beforeSend: function() {
      $("#data-area ul").append("加载中...");
    },
    success: function(json) {
      $("#data-area ul").empty();       //移除原来的分页数据
      totalItem = json.totalItem;
      pageSize = json.pageSize;
      curPage = page;
      totalPage = json.totalPage;
      var data_content = json.data_content;
      var data_html = "";
      $.each(data_content,function(index,array) {     //添加新的分页数据（数据的显示样式根据自己页面来设置，这里只是一个简单的列表）
        data_html += "<li>"+array['id']+"&nbsp;"+array['name']+"&nbsp;"+array['num']+"&nbsp;"+array['flag']+"</li>";
      });
 
      $("#data-area ul").append(data_html);
    },
    complete: function() {    //添加分页按钮栏
      getPageBar();
    },
    error: function() {
      alert("数据加载失败");
    }
  });
}
 
//获取分页条（分页按钮栏的规则和样式根据自己的需要来设置）
function getPageBar()
{
  if(curPage > totalPage) {
    curPage = totalPage;
  }
  if(curPage < 1) {
    curPage = 1;
  }
 
  pageBar = "";
 
  //如果不是第一页
  if(curPage != 1){
    pageBar += "<span class='pageBtn'><a href='javascript:turnPage(1)'>首页</a></span>";
    pageBar += "<span class='pageBtn'><a href='javascript:turnPage("+(curPage-1)+")'><<</a></span>";
  }
 
  //显示的页码按钮(5个)
  var start,end;
  if(totalPage <= 5) {
    start = 1;
    end = totalPage;
  } else {
    if(curPage-2 <= 0) {
        start = 1;
        end = 5;
    } else {
        if(totalPage-curPage < 2) {
            start = totalPage - 4;
            end = totalPage;
        } else {
            start = curPage - 2;
            end = curPage + 2;
        }
    }
  }
 
  for(var i=start;i<=end;i++) {
    if(i == curPage) {
        pageBar += "<span class='pageBtn-selected'><a href='javascript:turnPage("+i+")'>"+i+"</a></span>";
    } else {
        pageBar += "<span class='pageBtn'><a href='javascript:turnPage("+i+")'>"+i+"</a></span>";
    }
  }
   
  //如果不是最后页
  if(curPage != totalPage){
    pageBar += "<span class='pageBtn'><a href='javascript:turnPage("+(parseInt(curPage)+1)+")'>>></a></span>";
    pageBar += "<span class='pageBtn'><a href='javascript:turnPage("+totalPage+")'>尾页</a></span>";
  }
     
  $("#pageBar").html(pageBar);
}
 
//页面加载时初始化分页
$(function() {
  turnPage(1);
});